package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.UserDao;
import com.wdy.po.User;
import com.wdy.service.UserManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;
@SuppressWarnings("unchecked")
public class UserManagerImpl implements UserManager {

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void removeUser(User user) {
		userDao.delete(user);
	}

	public void updateUser(User user) {
		userDao.attachDirty(user);
	}

	@Override
	public void removeUsers(Object[] objects) {
		// TODO Auto-generated method stub
		userDao.deleteBatch(objects);
	}

	public User getUser(Integer id) {
		return userDao.findById(id);
	}

	public List<User> listAll() {
		return userDao.findAll();
	}

	public List<User> findByExample(User user) {
		return userDao.findByExample(user);
	}

	public List<User> findByName(String value) {
		return userDao.findByUsername(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = userDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) userDao.query(conList, page);
		List<User> users = new LinkedList<User>();
		for(Object o: tempList){
			if(o instanceof Object[]){
				Object[] oList = (Object[]) o;
				for(Object oo: oList){
					if(oo instanceof User){
						users.add((User) oo);
					}
				}
			}else{
				if(o instanceof User){
					users.add((User) o);
				}
			}
		}
		return new Result(page, users);
	}

}
